package org.example.netty.demo.chapter2;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * @author ： minzhiwei
 * create by 2020/1/13 19:47
 * @description
 */
public class TestClient {
    public static void main(String[] args) throws InterruptedException {
        final NioEventLoopGroup group = new NioEventLoopGroup();
        final ChannelFuture localhost = new Bootstrap().group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ChannelInboundHandler2());
                    }
                })
                .remoteAddress(new InetSocketAddress("localhost", 9999))
                .connect();
        final Channel channel = localhost.sync()
                .channel();
        channel.writeAndFlush("123123").addListener(future -> channel.close());
        channel.closeFuture().sync();
        group.shutdownGracefully();
    }
}
